package com.bt.att;

import com.bt.att.dao.PhoneDao;
import com.bt.att.model.BillingPeriod;
import com.bt.att.model.Call;
import com.bt.att.model.Message;
import com.bt.att.model.Phone;
import com.bt.att.parser.BillEntryProcessor;
import com.bt.att.parser.BillParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * DataImporter.
 *
 * @author Zakir Magdum
 */
@Configurable
@Component
public class DataImporter {

    @Autowired
    private PhoneDao dao;

    private DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mmaa");

    public void importFile(String fileName) {
         BillParser parser = new BillParser(fileName, new BillEntryImporter());
         parser.parse();
    }

    class BillEntryImporter implements BillEntryProcessor {
        @Override
        public void handle(String currentUser, String currentUserNumber, List<String> columns, List<String> values) {
            if (columns.size() > values.size()) {
                System.out.println("Columns " + columns.size() + " Values " + values.size());
                System.out.println(columns);
                System.out.println(values);
            }
            if (currentUser == null) {
                try {
                    Date start = BillParser.billingPeriodDateFormat.parse(values.get(0));
                    Date end = BillParser.billingPeriodDateFormat.parse(values.get(1));
                    BillingPeriod bp = new BillingPeriod(start, end);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else if (values.size() > 13) {
                String time = values.get(2) + " " + values.get(3);
                String id = values.get(0) + "-" + values.get(5) + "-" + time;
                Call call = new Call();
                call.setId(id);
                try {
                    call.setTimestamp(dateFormat.parse(time));
                } catch (ParseException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                call.setFrom(new Phone(currentUserNumber, currentUser, true));
                String location = values.get(6);
                boolean incoming = false;
                if (location.toLowerCase().startsWith("incoming")) {
                    incoming = true;
                    location = "";
                }
                Phone to = new Phone(values.get(5), null, false);
                to.setLocation(location);
                call.setTo(to);
                call.setIncoming(incoming);
                call.setDuration(Integer.parseInt(values.get(7)));
                call.setRateCode(values.get(8));
                call.setRatePaid(values.get(9));
                call.setFeature(values.get(10));
                call.setAirTimeCharge(Float.parseFloat(values.get(11)));
                call.setAdditionalCharge(Float.parseFloat(values.get(12)));
                call.setTotalCharge(Float.parseFloat(values.get(13)));
                dao.save(call);

            } else {
                String time = values.get(2) + " " + values.get(3);
                String id = values.get(0) + "-" + values.get(4) + "-" + time;
                Message msg = new Message();
                msg.setId(id);
                try {
                    msg.setTimestamp(dateFormat.parse(time));
                } catch (ParseException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                msg.setFrom(new Phone(currentUserNumber, currentUser, true));
                msg.setTo(new Phone(values.get(4), "", false));
                if (values.get(10).toLowerCase().equals("out")) {
                    msg.setIncoming(false);
                } else {
                    msg.setIncoming(true);
                }

                msg.setMsgKB(5);
                msg.setRateCode(values.get(7));
                msg.setRatePaid(values.get(8));
                msg.setFeature(values.get(9));
                msg.setTotalCharge(Float.parseFloat(values.get(11)));
                dao.save(msg);
            }
        }
    }


}
